package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户ID查询用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户ID查询用户信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    @GuiGuCache(prefix = "user:info:")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfo);
    }


    /**
     * 检查提交声音ID列表购买情况
     *
     * @param userId                    用户ID
     * @param albumId                   专辑ID
     * @param needCheckBuyStateTrackIds 待检查购买情况声音ID列表
     * @return 提交待检查购买声音ID购买结果 {38679:1,38678:0}
     */
    @Operation(summary = "检查提交声音ID列表购买情况")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(
            @PathVariable Long userId,
            @PathVariable Long albumId,
            @RequestBody List<Long> needCheckBuyStateTrackIds
    ) {
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, needCheckBuyStateTrackIds);
        return Result.ok(map);
    }


    /**
     * 查询当前用户是否购买指定专辑
     * @param albumId
     * @return true:已购买 false:未购买
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户是否购买指定专辑")
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
        Boolean isPaid = userInfoService.isPaidAlbum(albumId);
        return Result.ok(isPaid);
    }


    /**
     * 查询当前用户某个专辑下已购声音ID列表
     * @param albumId 专辑ID
     * @return 已购声音ID列表
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户某个专辑下已购声音ID列表")
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> getUserPaidTrackIdList(@PathVariable Long albumId){
        //1.获取当前登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务逻辑获取已购声音ID列表
        List<Long> trackIdList = userInfoService.getUserPaidTrackIdList(userId, albumId);
        return Result.ok(trackIdList);
    }


    /**
     * 用户付款成功后虚拟物品(VIP，专辑，声音)发货
     * @param userPaidRecordVo
     * @return
     */
    @Operation(summary = "用户付款成功后虚拟物品发货")
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }

    /**
     * 更新VIP状态：处理过期会员
     * @return
     */
    @Operation(summary = "更新VIP状态：处理过期会员")
    @GetMapping("/updateVipExpireStatus")
    public Result updateVipExpireStatus(){
        userInfoService.updateVipExpireStatus(new Date());
        return Result.ok();
    }
}

